<template>
  <div class="body">
    <div class="stars" ref="starsRef">
      <div class="star" v-for="(item, index) in starsCount" :key="index"></div>
    </div>
    <div id="building">
    <div class="handle-box">
      <el-select v-model="searchid" placeholder="请选择筛选方式">
        <el-option v-for="item in options1" :label="item.label" :key="item.key" :value="item.value">
        </el-option>
      </el-select>
      <el-input placeholder="筛选信息" v-model="searchWord" style="width: 100px"></el-input>
      <el-button type="primary" @click="searchfor(searchid)">查询信息</el-button>
      <el-button type="primary" @click="dialogVisible1= true">添加信息</el-button>
    </div>
    <el-table height="550px" border size="small" :data="drugData.slice((currentpage - 1)*pagesize, currentpage * pagesize)" style="width: 100%" >
      <el-table-column label="药品编号" prop="drugNumber" width="140" align="center"></el-table-column>
      <el-table-column label="药物名称" prop="drugName" width="140" align="center"></el-table-column>
      <el-table-column label="厂家" prop="factory" width="140" align="center"></el-table-column>
      <el-table-column label="处方类型" prop="ptype" width="140" align="center"></el-table-column>
      <el-table-column label="价格" prop="price" width="140" align="center"></el-table-column>
      <el-table-column label="库存" prop="inventory" width="140" align="center"></el-table-column>
      <el-table-column   label="操作" width="140" align="center">
        <template slot-scope="scope">
          <el-button @click.native.prevent="editRow(scope.row)" type="text" size="small">
            修改
          </el-button>
        </template>
      </el-table-column>
      <el-table-column   label="操作" width="140" align="center">
        <template slot-scope="scope">
          <el-button @click.native.prevent="deleteRow(scope.row.drugNumber)" type="text" size="small">
            移除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, prev, pager, next, jumper"
      :current-page="currentpage"
      :page-size="pagesize"
      :total="drugData.length"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
    </div>
    <!-- 添加弹窗-->
    <el-dialog title="增加信息"
               :visible.sync="dialogVisible1"
               width="30%" center>
      <el-form label-width="80px" :model="registerForm"  :rules="rules">
        <el-form-item label="药品编号" prop="drugNumber">
          <el-input v-model="registerForm.drugNumber"></el-input>
        </el-form-item>
        <el-form-item label="药物名称" prop="drugName">
          <el-input v-model="registerForm.drugName"></el-input>
        </el-form-item>
        <el-form-item label="厂家" prop="factory">
          <el-input v-model="registerForm.factory"></el-input>
        </el-form-item>
        <el-form-item label="处方类型" prop="ptype">
          <el-input  v-model="registerForm.ptype"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input type="textarea" v-model="registerForm.price"></el-input>
        </el-form-item>
        <el-form-item label="库存量" prop="inventory">
          <el-input  v-model="registerForm.inventory"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible1=false">取 消</el-button>
        <el-button type="primary" @click="adddrug()">确 定</el-button>
      </span>
      </template>
    </el-dialog>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑信息" :visible.sync="dialogVisible2"
               width="30%" center>
      <el-form label-width="80px" :model="editForm"  :rules="rules">
        <el-form-item label="药品编号" prop="drugNumber">
          <el-input v-model="editForm.drugNumber" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="药物名称" prop="drugName">
          <el-input v-model="editForm.drugName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="厂家" prop="factory">
          <el-input v-model="editForm.factory" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="处方类型" prop="ptype">
          <el-input  v-model="editForm.ptype"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input type="textarea" v-model="editForm.price"></el-input>
        </el-form-item>
        <el-form-item label="库存量" prop="inventory">
          <el-input   v-model="editForm.inventory" ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible2=false">取 消</el-button>
        <el-button type="primary" @click="saveEdit()">确 定</el-button>
      </span>
      </template>
    </el-dialog>

    <!-- 移除提示框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible3"
               width="30%"  center>
      <div class="del-dialog-cnt" align="center">是否确定移除？</div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible3=false">取 消</el-button>
          <el-button type="primary" @click="deletedrug()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {onMounted, ref} from "vue";

export default {
  name: "message",
  data(){
    return{
      drugData:[
        {
          drugNumber:'2',
          drugName:'熊诗伟',
          factory:'女',
          ptype:'21',
          price:'6.1',
          inwentory:'1'
        },
        {
          drugNumber:'2',
          drugName:'熊诗伟',
          factory:'女',
          ptype:'21',
          price:'6.1',
          inwentory:'1'
        },
        {
          drugNumber:'2',
          drugName:'熊诗伟',
          factory:'女',
          ptype:'21',
          price:'6.1',
          inwentory:'1'
        },
      ],
      options1:[
        {
          value:'1',
          label:"药品编号",
          key:'1'
        },
        {
          label: "药物名称",
          value:'2',
          key:'2'
        },
        {
          value:'3',
          label: "厂家",
          key:'3'
        },
        {
          value:'4',
          label:"处方类型",
          key:'4'
        },
        ],
      searchid:'',
      searchWord:'',
      pagesize:'9',
      currentpage:'1',
      dialogVisible1:false,
      dialogVisible2:false,
      dialogVisible3:false,
      editForm:{
        drugNumber:'',
        drugName:'',
        factory:'',
        ptype:'',
        price:'',
        inventory:''
      },
      registerForm:{
        drugNumber:'',
        drugName:'',
        factory:'',
        ptype:'',
        price:'',
        inventory:''
      },
      rules: {
        drugNumber: [
          {required: true, message: " 药物编号不可为空", trigger: 'blur'}
        ],
       drugName: [
          {required: true, message: " 药物名称不能为空 ", trigger: 'blur'}
        ],
        factory: [
          {required: true, message: " 厂家不可为空 ", trigger: 'blur'}
        ],
        ptype: [
          {required: true, message: " 处方类型不可为空 ", trigger: 'blur'}
        ],
        price: [
          {required: true, message: " 价格不可为空 ", trigger: 'blur'}
        ],
        inventory: [
          {required: true, message: " 库存不可为空 ", trigger: 'blur'}
        ]
      },
      deleteId:'',
    }
  },
  created(){
    axios.get('http://localhost:8080/Drug/queryDrugList',{params:{startIndex:0,pageSize:1000}}).then(res=>{
      this.drugData=res.data.drugData;
    })
  },
  methods:{
    handleSizeChange(val) { //每页条数
      this.pagesize = val;
    },
    handleCurrentChange(val) { //跳转页码
      this.currentpage= val;
    },
    searchfor(id){
      let xxd1='';
      let xxd2='';
      let xxd3='';
      let xxd4='';
      if(id==1){
        xxd1=this.searchWord;
      }
      else if(id==2){
        xxd2=this.searchWord;
      }
      else if(id==3){
        xxd3=this.searchWord;
      }else{
        xxd4=this.searchWord;
      }
      axios.get('http://localhost:8080/Drug/queryDrugByKey',{params:{startIndex:0,pageSize:1000,drugNumber:xxd1,drugName:xxd2,factory:xxd3,pType:xxd4}}).then(res=>{
        this.drugData=res.data.drugData;
      })
    },
    adddrug(){
      axios.post('http://localhost:8080/Drug/insertDrug',this.registerForm).then(res=>{
        if(res.data==1){
          this.$message({
            message: '新增成功'
          });
          this.$router.go(0);
        }
        else if(res.data==0){
          this.$message({
            message: '增加失败',
            type:'warning'
          });
        }
        else{
          this.$message({
            message: '编号重复',
            type:'warning'
          });
        }
      });
      this.dialogVisible1=false
    },
    saveEdit(){
      axios.put('http://localhost:8080/Drug/updateDrug',this.editForm).then(res=>{
        if(res.data==1){
          this.$message({
            message: '编辑成功'
          });
          this.$router.go(0);
        }
        else{
          this.$message({
            message: '编辑失败',
            type:'warning'
          });
        }
      });
      this.dialogVisible2=false
    },
    deletedrug(){
      let drugNumber=this.deleteId
      axios.delete('http://localhost:8080/Drug/deleteDrug',{data:drugNumber}).then(res=>{
        if(res.data==1){
          this.$message({
            message: '删除成功'
          });
          this.$router.go(0);
        }
        else{
          this.$message({
            message: '删除失败,没有该药',
            type:'warning'
          });
        }
      });
      this.dialogVisible3=false
    },
    editRow(row){
      this.dialogVisible2 = true;
      this.editForm.drugNumber=row.drugNumber;
      this.editForm.drugName=row.drugName;
      this.editForm.factory=row.factory;
      this.editForm.ptype=row.ptype;
      this.editForm.price=row.price;
      this.editForm.inventory=row.inventory;
    },
    deleteRow(id){
      this.dialogVisible3 = true;
      this.deleteId=id;
    },
  },
  setup() {
    let starsRef = ref(null);

    const starsCount = 800; //星星数量
    const distance = 900; //间距

    onMounted(() => {
      let starNodes = Array.from(starsRef.value.children);
      starNodes.forEach((item) => {
        let speed = 0.2 + Math.random() * 1;
        let thisDistance = distance + Math.random() * 300;
        item.style.transformOrigin = `0 0 ${thisDistance}px`;
        item.style.transform = `
        translate3d(0,0,-${thisDistance}px)
        rotateY(${Math.random() * 360}deg)
        rotateX(${Math.random() * -50}deg)
        scale(${speed},${speed})`;
      });
    });

    return {
      starsRef,
      starsCount,
    };
  },
}
</script>

<style scoped>
#building {
  /*设置透明度，0为完全透明，1为不透明*/
  opacity: 0.50;
}
.body {
  position: absolute;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  background: radial-gradient(
    200% 100% at bottom center,
    #f7f7b6,
    #e96f92,
    #1b2947
  );
  background: radial-gradient(
    200% 105% at top center,
    #1b2947 10%,
    #75517d 40%,
    #e96f92 65%,
    #f7f7b6
  );
  background-attachment: fixed;
  overflow: hidden;
}

@keyframes rotate {
  0% {
    transform: perspective(400px) rotateZ(20deg) rotateX(-40deg) rotateY(0);
  }
  100% {
    transform: perspective(400px) rotateZ(20deg) rotateX(-40deg)
    rotateY(-360deg);
  }
}
.stars {
  transform: perspective(500px);
  transform-style: preserve-3d;
  position: absolute;
  perspective-origin: 50% 100%;
  left: 45%;
  animation: rotate 90s infinite linear;
  bottom: 0;
}
.star {
  width: 2px;
  height: 2px;
  background: #f7f7b6;
  position: absolute;
  left: 0;
  top: 0;
  backface-visibility: hidden;
}
</style>
